using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using limpalex.bbparser.Tokens;

namespace limpalex.bbparser.Tree
{
    internal sealed class Leaf : INode
    {
        private const string UrlRegexText =
            @"(?<url>((http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&amp;%\$#\=~])*)|(www\.[^\s]+))";

        private static readonly Regex UrlRegex = 
            new Regex(UrlRegexText, RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);

        public void GetText(StringBuilder buffer, Parser parser)
        {
            //string value = text;

            /*if (FindUrls)
                    value = UrlRegex.Replace(value, 
                        delegate(Match match)
                        {
                            string s = match.Groups["url"].Value;
                            string url = s;
                            if ( url.StartsWith("www."))
                                url = "http://" + url;
                            else
                            {
                                s = s.Replace("http://", string.Empty).
                                    Replace("https://", string.Empty).Replace("ftp://",string.Empty);
                            }
                            return string.Format("<a href='{1}' target='_blank'>{0}</a>", s, url);
                        });*/

            buffer.Append(text);
        }

        public NodeType Type
        {
            get { return NodeType.Leaf; }
        }

        private readonly string text;

        public Leaf(string text)
        {
            this.text = text;
        }

        public override string ToString()
        {
            return text;
        }
    }
}